/**
 * 
 */
$(document).ready(function() {

	var idNodes = $(".trNode");
	if(idNodes.length == 0 || idNodes == null){
		alert("购物车为空");
		history.back(-1);
	}
	
	
	//跳转到结算中心
	$('#buy').click(function(){
		var classSum = $("#classSum").html();
		var countSum = $("#countSum").html();
		var priceSum = $("#priceSum").html();
		var id = $(".myTable").attr("id");
		var ids = [];
		for (var i = 0; i < idNodes.length; i++) {
			ids[i] = idNodes.eq(i).attr("id");
		}		
		var countNodes = $(".count");
		for (var i = 0; i < countNodes.length; i++) {
			countss[i] = countNodes.eq(i).val();
			idss[i] = countNodes.eq(i).parent().parent().attr("id");
			//alert("countss[i] = " + countss[i] + "    idss[i] = " +idss[i]);
		}
		$.post(//修改购物车
				"car/editCar",
				{countss:countss,ids:idss},
				//{"counts":countss,"ids":idss},
				function(result){
					$.post(
						"car/jumpBalance",
						{"id":id,"classSum":classSum,"countSum":countSum,"priceSum":priceSum,ids:ids},
						function(){
							window.parent.document.getElementById("leftIframe").src="vip.jsp";
							window.location.href="balance.jsp?id="+id+"&classSum="+classSum+"&countSum="+countSum+"&priceSum="+priceSum;
						}
						//{"counts":countss,"ids":idss},
					)
				}
			)
		//window.location.href="jumpBalance?id="+id+"&classSum="+classSum+"&countSum="+countSum+"&priceSum="+priceSum+"&ids="+ids;
	});
/*	function init(){
		var price = $("#price").attr("html");
		var count = $("#count").val();
		var classSum = $("#classSum").attr("html");
		var countSum = $("#countSum").attr("html");
		var price = $("#priceSum").attr("html");
	}
*/
	$(".count").change(function(){
		var parent = $(this).parent().parent();
		var count = parent.find(".count").val();
		var price = parent.find("#price").html();
		var total = price * count;
		parent.find(".total").html(total+".0");

		var countSum = 0;// = $("#countSum"); 
		var priceSum = 0;// = $("#priceSum"); 
		var counts = $(".count");
		var totals = $(".total");
		for (var i = 0; i < counts.length; i++) {
			countSum = countSum +  parseInt(counts.eq(i).val());
			priceSum = priceSum +  parseInt(totals.eq(i).html());
		}
		$("#countSum").html(countSum);
		$("#priceSum").html(priceSum+".0");
	});
	
	//继续购买
	var countss = [];
	var idss = [];
	$("#continueShop").click(function(){
		var countNodes = $(".count");
		for (var i = 0; i < countNodes.length; i++) {
			countss[i] = countNodes.eq(i).val();
			idss[i] = countNodes.eq(i).parent().parent().attr("id");
			//alert("countss[i] = " + countss[i] + "    idss[i] = " +idss[i]);
		}
		$.post(//修改购物车
			"car/editCar",
			{countss:countss,ids:idss},
			//{"counts":countss,"ids":idss},
			function(result){
				history.back(-1);
			}
		)
		history.back(-1);
	})
	//删除一条购物车数据
	$(".delete").click(function(){
		var id = $(this).parent().parent().attr("id");
		$.get(
			"car/deleteCar",
			{"id":id},
			function(){
				window.location.reload(true);
			}
		)
	});
	//清除购物车
	$("#clearCar").click(function(){
		var uid = $(".myTable").attr("id");
		$.get(
			"car/clearCar",
			{"uid":uid},
			function(){
				window.location.reload(true);
			}
		)
	});
	
});